home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / REXP1.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  4KB  |  188 lines

  1.  
  2. /********************************************
  3. rexp1.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log:    rexp1.c,v $
  14.  * Revision 3.2  91/08/13  09:10:11  brennan
  15.  * VERSION .9994
  16.  * 
  17.  * Revision 3.1  91/06/07  10:33:22  brennan
  18.  * VERSION 0.995
  19.  * 
  20. */
  21.  
  22. /*  re machine  operations  */
  23.  
  24. #include  "rexp.h"
  25.  
  26. static MACHINE *PROTO( new_TWO , (int) ) ;
  27.  
  28.  
  29. static  MACHINE  *new_TWO(type)
  30.   int type ;
  31.   static  MACHINE  x ;
  32.  
  33.   x.start = (STATE *) RE_malloc(2*STATESZ) ;
  34.   x.stop = x.start + 1 ;
  35.   x.start->type = type ;
  36.   x.stop->type = M_ACCEPT ;
  37.   return &x ;
  38. } ;
  39.  
  40.  
  41. /*  build a machine that recognizes any  */
  42. MACHINE  RE_any()
  43. { return  * new_TWO(M_ANY) ; }
  44.  
  45. /*  build a machine that recognizes the start of string  */
  46. MACHINE  RE_start()
  47. { return  * new_TWO(M_START) ; }
  48.  
  49. MACHINE  RE_end()
  50. { return  * new_TWO(M_END) ; }
  51.  
  52. /*  build a machine that recognizes a class  */
  53. MACHINE  RE_class( bvp )
  54.   BV *bvp  ;
  55. { register MACHINE *p = new_TWO(M_CLASS) ;
  56.  
  57.   p->start->data.bvp = bvp ;
  58.   return *p ;
  59. }
  60.  
  61.  
  62. MACHINE  RE_u()
  63. { return  *new_TWO(M_U) ; }
  64.  
  65. MACHINE  RE_str( str, len)
  66.   char *str ;
  67.   unsigned len ;
  68. { register MACHINE *p = new_TWO(M_STR) ;
  69.  
  70.   p->start->len = len ;
  71.   p->start->data.str = str ;
  72.   return *p ;
  73. }
  74.  
  75. /*  replace m and n by a machine that recognizes  mn   */
  76. void  RE_cat( mp, np)
  77.   MACHINE  *mp, *np ;
  78. { unsigned sz1, sz2, sz ;
  79.  
  80.   sz1 = mp->stop - mp->start  ;
  81.   sz2 = np->stop - np->start + 1 ;
  82.   sz  = sz1 + sz2 ;
  83.  
  84.   mp->start = (STATE *) RE_realloc( mp->start, sz * STATESZ ) ;
  85.   mp->stop = mp->start + (sz - 1) ;
  86.   (void)  memcpy( mp->start + sz1, np->start, SIZE_T(sz2 * STATESZ) ) ;
  87.   free( np->start ) ;
  88. }
  89.  
  90.  /*  replace m by a machine that recognizes m|n  */
  91.  
  92. void  RE_or( mp, np)
  93.   MACHINE  *mp, *np ;
  94. { register STATE *p ;
  95.   unsigned szm, szn ;
  96.  
  97.   szm = mp->stop - mp->start + 1 ;
  98.   szn = np->stop - np->start + 1 ;
  99.  
  100.   p = (STATE *) RE_malloc( (szm+szn+1) * STATESZ ) ;
  101.   (void) memcpy( p+1, mp->start, SIZE_T(szm * STATESZ) ) ;
  102.   free( mp->start) ;
  103.   mp->start = p ;
  104.   (mp->stop  = p + szm + szn) -> type = M_ACCEPT ;
  105.   p->type = M_2JA ;
  106.   p->data.jump = szm+1 ;
  107.   (void) memcpy( p + szm + 1 , np->start, SIZE_T(szn * STATESZ)) ;
  108.   free( np->start ) ;
  109.   (p += szm)->type = M_1J ;
  110.   p->data.jump = szn ;
  111. }
  112.  
  113. /*  UNARY  OPERATIONS     */
  114.  
  115. /*  replace m by m*   */
  116.  
  117. void  RE_close( mp )
  118.   MACHINE  *mp ;
  119. { register STATE *p ;
  120.   unsigned sz ;
  121.  
  122.   sz = mp->stop - mp->start + 1 ;
  123.   p = (STATE *) RE_malloc( (sz+2) * STATESZ ) ;
  124.   (void) memcpy( p+1, mp->start, SIZE_T(sz * STATESZ)) ;
  125.   free( mp->start ) ;
  126.   mp->start = p ;
  127.   mp->stop  = p + (sz+1) ;
  128.   p->type = M_2JA ;
  129.   p->data.jump = sz + 1 ;
  130.   (p += sz) -> type = M_2JB ;
  131.   p->data.jump = -(sz-1) ;
  132.   (p+1)->type = M_ACCEPT ;
  133. }
  134.  
  135. /*  replace m  by  m+  (positive closure)   */
  136.  
  137. void  RE_poscl( mp )
  138.   MACHINE  *mp ;
  139. { register STATE *p ;
  140.   unsigned  sz ;
  141.  
  142.   sz = mp->stop - mp->start + 1 ;
  143.   mp->start = p = (STATE *) RE_realloc(mp->start ,  (sz+1) * STATESZ ) ;
  144.   mp->stop  = p + sz ;
  145.   p +=  --sz ;
  146.   p->type = M_2JB ;
  147.   p->data.jump = -sz ;
  148.   (p+1)->type = M_ACCEPT ;
  149. }
  150.  
  151. /* replace  m  by  m? (zero or one)  */
  152.  
  153. void  RE_01( mp )
  154.   MACHINE  *mp ;
  155. { unsigned  sz ;
  156.   register  STATE *p ;
  157.  
  158.   sz = mp->stop - mp->start + 1 ;
  159.   p = (STATE *) RE_malloc( (sz+1) * STATESZ ) ;
  160.   (void) memcpy( p+1, mp->start, SIZE_T(sz * STATESZ)) ;
  161.   free( mp->start ) ;
  162.   mp->start = p ;
  163.   mp->stop = p + sz ;
  164.   p->type = M_2JB ;
  165.   p->data.jump = sz ;
  166. }
  167.  
  168. /*===================================
  169. MEMORY  ALLOCATION
  170.  *==============================*/
  171.  
  172.  
  173. VOID *RE_malloc( sz ) 
  174.   unsigned sz ;
  175. { register VOID *p ;
  176.  
  177.   if ( ! ( p = malloc(SIZE_T(sz)) ) )  RE_error_trap(MEMORY_FAILURE) ;
  178.   return p ;
  179. }
  180.  
  181. VOID *RE_realloc( p, sz)
  182.   register VOID *p ; unsigned sz ;
  183. { if ( ! ( p = realloc( p, SIZE_T(sz)) ) )  RE_error_trap(MEMORY_FAILURE) ;
  184.   return p ;
  185. }
  186.  
  187.